package org.example.heartbeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @author xuan
 * @create 2023/9/18
 */
public class MyServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        String strEventType="";
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent)evt;
            switch (event.state()){
                case ALL_IDLE:
                    strEventType="读写空闲";
                    break;
                case READER_IDLE:
                    strEventType="读空闲";
                    break;
                case WRITER_IDLE:
                    strEventType="写空闲";
                    break;
            }
            System.out.println(ctx.channel().remoteAddress() + "--超时事件--" + strEventType);
            System.out.println("服务器处理中");

            //如果不关闭 上述空闲会频发的发送
            //ctx.channel().close();
        }
    }
}
